package com.jay.media.music.ui.home

import android.os.Bundle
import android.util.Log
import android.view.*
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.appbar.AppBarLayout
import com.jay.base.ui.BaseFragment
import com.jay.media.kit.ColorUtils
import com.jay.media.kit.LogHelper
import com.jay.media.kit.ToolbarColorHelper
import com.jay.media.music.ui.HomeActivity
import com.jay.media.music.ui.R
import com.jay.media.music.core.constant.MusicListType
import com.jay.media.music.ui.play.MusicListActivity
import com.jay.media.music.ui.views.AppBarStateChangeListener

class HomeFragment : BaseFragment(), View.OnClickListener {

    private lateinit var homeViewModel: HomeViewModel
    private var mToolBar: Toolbar? = null
    private var mToolbarLayout: AppBarLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)
        val root: View = inflater.inflate(R.layout.fragment_home, null)
        initView(root)
        initToolbar()
        return root
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        inflater.inflate(
            R.menu.launcher,
            menu
        )
    }

    override fun initView(view: View) {
        mToolBar = view.findViewById(R.id.toolbar)
        mToolbarLayout = view.findViewById(R.id.toolbarLayout)
        val playHistoryTextView = view.findViewById<TextView>(R.id.playHistoryTextView)
        val playListTextView = view.findViewById<TextView>(R.id.playListTextView)
        val favorTextView = view.findViewById<TextView>(R.id.favorTextView)
        playHistoryTextView.setOnClickListener(this)
        playListTextView.setOnClickListener(this)
        favorTextView.setOnClickListener(this)
    }

    private fun initToolbar() {
        mToolbarLayout?.addOnOffsetChangedListener(object : AppBarStateChangeListener() {
            override fun onStateChanged(appBarLayout: AppBarLayout?, state: State?) {
                LogHelper.d(TAG, "expand state $state")
                var color = ColorUtils.getColor(R.color.toolbarExpandColor)
                if (state == State.COLLAPSED) {
                    color = ColorUtils.getColor(R.color.toolbarCollapsedColor)
                }
                ToolbarColorHelper.colorToolbar(
                    mToolBar, color, mActivity
                )
            }
        })
        (mActivity as HomeActivity).setupToolbarNavBar(mToolBar!!)
        ToolbarColorHelper.colorToolbar(
            mToolBar,
            ColorUtils.getColor(R.color.toolbarExpandColor),
            mActivity
        )
    }

    override fun onContextItemSelected(item: MenuItem): Boolean {
        Log.d(TAG, "onContextItemSelected() called with: item = $item")
        return super.onContextItemSelected(item)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        Log.d(TAG, "onOptionsItemSelected() called with: item = $item")
        return super.onOptionsItemSelected(item)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.playHistoryTextView -> {
                MusicListActivity.start(mActivity, MusicListType.TYPE_PLAY_HISTORY)
            }
            R.id.favorTextView -> {
                MusicListActivity.start(mActivity, MusicListType.TYPE_FAVOR_LIST)
            }
            R.id.playListTextView -> {
                MusicListActivity.start(mActivity, MusicListType.TYPE_PLAY_LIST)
            }
            else -> {
            }
        }
    }
}